<?php

namespace app\controller\backed;

use app\BaseController;
use app\model\Notice as NoticeModel;


class Notice extends BaseController
{

    public function getNotices()
    {
        $params = request()->param();
        
        $where = [];

        if(isset($params['title'])){
            $where[] = ['title', 'like','%' . $params['title'] . '%'];
        }

        $pageNo = isset($params['pageNo']) ? $params['pageNo'] : 1;
        $pageSize = isset($params['pageSize']) ? $params['pageSize'] : 20;


        $res = NoticeModel::where($where)->paginate([
            'page' => $pageNo,
            'list_rows' => $pageSize
        ],false);

        return $this->successMessage('查询成功',$res);
    }

    public function createNotice()
    {       

        $params = request()->param();

        $data = [
        	'title' => $params['title'],
			'desc' => $params['desc'],
			'link' => $params['link'],
        ];

        $NoticeModel = new NoticeModel();
        $NoticeModel->save($data);

        return $this->successMessage('创建成功');

    }


    public function updateNotice()
    {

        $params = request()->param();

        $data = [
            'title' => $params['title'],
            'desc' => $params['desc'],
            'link' => $params['link'],
			'update_time' => time(),
        ];

        $noticeModel = new NoticeModel();
        $noticeModel->update($data, ['id' => $params['id']]);

        return $this->successMessage('修改成功');
    }


    public function deleteNotice()
    {

        $params = request()->param();
        NoticeModel::destroy($params['id']);
        
        return $this->successMessage('删除成功');
    }


}